package com.dg.river.core.util.glide;

import android.app.Activity;
import android.widget.ImageView;

import androidx.fragment.app.Fragment;

import com.bumptech.glide.Glide;
import com.bumptech.glide.Priority;
import com.bumptech.glide.RequestManager;
import com.bumptech.glide.load.engine.DiskCacheStrategy;
import com.bumptech.glide.request.RequestOptions;
import com.dg.river.R;


/**
 * Created by hy on 2018/11/30
 * Description : glide 加载图片工具类
 */
public class GlideShowUtils {

    private Activity activity;
    private Fragment fragment;
    private RequestManager manager;

    public GlideShowUtils(Activity activity) {
        this.activity = activity;
        manager = Glide.with(activity);
    }

    public GlideShowUtils(Fragment fragment) {
        this.fragment = fragment;
        manager = Glide.with(fragment);
    }

    /**
     * glide 加载图片
     * 普通加载
     *
     * @param url       图片路径
     * @param imageView 显示控件
     */
    public static void GlidePicture(RequestManager glide, String url, ImageView imageView) {
        glide.load(url).into(imageView);
    }

    /**
     * glide 加载图片
     * 可控制缓存，占位图为默认图
     *
     * @param url       图片路径
     * @param imageView 显示控件
     */
    public static void GlidePicture(RequestManager glide, String url, ImageView imageView, boolean hasCache) {
        GlidePicture(glide, url, imageView, R.mipmap.ic_launcher, hasCache);
    }

    /**
     * glide 加载图片
     * 可控制缓存，随意设置
     *
     * @param url             图片路径
     * @param imageView       显示控件
     * @param placeHolderView 占位图
     */
    public static void GlidePicture(RequestManager glide, String url, ImageView imageView, int placeHolderView, boolean hasCache) {
        if (hasCache) {
            RequestOptions requestOptions = new RequestOptions()
                    .diskCacheStrategy(DiskCacheStrategy.DATA)
                    .priority(Priority.HIGH)
                    .placeholder(placeHolderView);
            glide.load(url).apply(requestOptions).thumbnail(0.1f).into(imageView);
        } else {
            RequestOptions requestOptions = new RequestOptions()
                    .placeholder(placeHolderView);
            glide.load(url).apply(requestOptions).into(imageView);
        }
    }

    /**
     * glide 加载图片
     * 可控制缓存，随意设置
     *
     * @param url             图片路径
     * @param imageView       显示控件
     * @param placeHolderView 占位图
     */
    public static void GlidePicture(RequestManager glide, String url, ImageView imageView, int placeHolderView, int errorView, boolean hasCache) {
        if (hasCache) {
            RequestOptions requestOptions = new RequestOptions()
                    .diskCacheStrategy(DiskCacheStrategy.DATA)
                    .priority(Priority.HIGH)
                    .placeholder(placeHolderView)
                    .error(errorView);
            glide.load(url).apply(requestOptions).thumbnail(0.1f).into(imageView);
        } else {
            RequestOptions requestOptions = new RequestOptions()
                    .placeholder(placeHolderView)
                    .error(errorView);
            glide.load(url).apply(requestOptions).into(imageView);
        }
    }

    /**
     * glide 加载图片
     *
     * @param drawable  图片路径
     * @param imageView 显示控件
     */
    public static void GlidePicture(RequestManager glide, int drawable, ImageView imageView) {
        glide.load(drawable).into(imageView);
    }
}
